/**
 * Created by zyd on 2015/10/20.
 */
;
(function ($) {

    "use strict";

    var DataExport = function (options) {
        this.setting = options;
    };
    DataExport.prototype = {
        openDialog: function () {
            var _setting = this.setting;
            $("#export_win").window("destroy");
            $("<div id='export_win'></div>").dialog({
                width: _setting.width||400,
                height:_setting.height|| 200,
                closed: false,
                modal: true,
                title: '数据导出',
                loadingMessage: '',
                href: this.setting.href,
                buttons: [
                    {
                        text: '导出',
                        iconCls: 'icon-search',
                        handler: function () {
                            var input1 = $("<input>");
                            input1.attr('type', 'hidden');
                            input1.attr('name', 'excelInfo.template');
                            input1.attr('value', _setting.template);
                            $("#export_win form").append(input1);
                            $("#export_win form").attr("action", _setting.target)
                            $("#export_win form").attr('style', 'display:none');
                            $("#export_win form").attr('target', '');
                            $("#export_win form").attr('method', 'post');
                            $("#export_win form").submit();
                            $('#export_win').window("close");
                        }
                    },
                    {
                        text: '关闭',
                        handler: function () {
                            $('#export_win').window("close");
                        }
                    }
                ]
            });
        },
        createForm: function () {
            var _setting = this.setting;
            var template = _setting.template;
            //向后台发送请求
            var form = $("<form>");//定义一个form表单
            form.attr('style', 'display:none');
            form.attr('target', '');
            form.attr('method', 'post');
            form.attr('action', _setting.target);
            //添加input
            var input1 = $("<input>");
            input1.attr('type', 'hidden');
            input1.attr('name', 'excelInfo.template');
            input1.attr('value', template);
            //将表单放到body中
            $('body').append(form);
            form.append(input1);
            form.submit();//提交表单
            form.remove();
        }

    };
    DataExport.exportWithCondition = function (options) {
        var _this_ = this;
        new _this_(options).openDialog();
    };
    DataExport.exportAll = function (options) {
        var _this_ = this;
        new _this_(options).createForm();

    };
    window["DataExport"] = DataExport;
})(jQuery);
